<?php

namespace app\common\dao\approval;

use app\common\dao\BaseDao;
use app\common\model\approval\ApprovalFlowModel;

class ApprovalFlowDao extends BaseDao
{

    /**
     * @return ApprovalFlowModel
     */
    protected function getModel(): string
    {
        return ApprovalFlowModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {

        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                if (is_array($where['is_type'])) {
                    $query->whereIn('is_type', $where['is_type']);
                } else {
                    $query->where('is_type', (int)$where['is_type']);
                }
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', intval($where['status']));
            })
            ->when(isset($where['source_id']) && $where['source_id'] !== '', function ($query) use ($where) {
                $query->where('source_id', intval($where['source_id']));
            })
            ->when(isset($where['submit_uid']) && $where['submit_uid'] !== '', function ($query) use ($where) {
                $query->where('submit_uid', intval($where['submit_uid']));
            })
            ->when(isset($where['present_uid']) && $where['present_uid'] !== '', function ($query) use ($where) {
                $query->where('present_uid', intval($where['present_uid']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            });
    }
}